package project.ia04.gui.frames;

import jade.gui.GuiEvent;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.ComponentOrientation;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.border.BevelBorder;

import project.ia04.agents.gui.XAgent;
import project.ia04.agents.gui.XCommandAgent;
import project.ia04.gui.components.ControlsPanel;
import project.ia04.gui.components.EndDialog;
import project.ia04.gui.components.HistoricalDialog;
import project.ia04.gui.components.NewProjectDialog;
import project.ia04.gui.components.StaffListChooserDialog;
import project.ia04.gui.components.StaffPanel;
import project.ia04.gui.components.StatusBar;
import project.ia04.utils.Data;
import project.ia04.utils.Staff;
import project.ia04.utils.VideoGame;

public class MainFrame extends JFrame implements PropertyChangeListener{
	
	private ScreenSettings screenSettings = ScreenSettings.getInstance();
	private JScrollPane scrollPane;
	private ControlsPanel controlsPanel = new ControlsPanel(this);
	private StatusBar statusBar = new StatusBar();
	
	private GridBagLayout staffsLayout = new GridBagLayout();
	private JPanel staffsPanel = new JPanel(staffsLayout);
	private int gridCurrY = 0;
	private int gridCurrX = 0;
	
	private ArrayList<StaffPanel> staffPanelList = new ArrayList<StaffPanel>();
	private XAgent agent;
	
	public MainFrame(){
		JPanel panelOptionPane = new JPanel(new BorderLayout());
		JLabel titleOptionPane = new JLabel("Entre une nom :");
		JTextField nameOptionPane = new JTextField();
		panelOptionPane.add(titleOptionPane,BorderLayout.NORTH);
		panelOptionPane.add(nameOptionPane);
		int ret = JOptionPane.showOptionDialog(null, panelOptionPane,"Nom de l'entreprise",JOptionPane.OK_CANCEL_OPTION ,JOptionPane.INFORMATION_MESSAGE,new ImageIcon(""), null,null);
		if(ret == JOptionPane.CANCEL_OPTION || ret == JOptionPane.CLOSED_OPTION)
			System.exit(0);
		setTitle(nameOptionPane.getText());
		
		
		setLocation(screenSettings.getCenterPosition().x-500,screenSettings.getCenterPosition().y-293);
		setSize(1000,615);
		setResizable(false);
		setLayout(new GridBagLayout());
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		buildStaffPanel();
		
		GridBagConstraints c = new GridBagConstraints();
		c.gridx = 0;
		c.gridy = 0;
		c.fill = GridBagConstraints.BOTH;
		c.weighty = 1.0;
		c.gridheight = GridBagConstraints.REMAINDER;
		add(controlsPanel,c);
		c.gridx = 1;
		c.gridy = 1;
		c.weighty = 0.0;
		add(statusBar,c);
		
		
		
		setListener();
		
		buildTheme();
		
		setVisible(true);
	}


	private void buildStaffPanel() {
		
		staffsPanel.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
		scrollPane = new JScrollPane(staffsPanel,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		scrollPane.setColumnHeaderView(new JLabel("Equipe",JLabel.CENTER));
		
		GridBagConstraints c = new GridBagConstraints();
		c.gridx = 1;
		c.gridy = 0;
		c.fill = GridBagConstraints.BOTH;
		c.anchor = GridBagConstraints.NORTH;
		c.weightx = 1.0;
		c.weighty = 1.0;
		
		add(scrollPane,c);
	}

	public void addNewStaff(String function,String name,int lvl){
		StaffPanel staffPanel = new StaffPanel(function,name,lvl);
		
		GridBagConstraints c; 
		if(staffPanelList.size() > 0){
			c = staffsLayout.getConstraints(staffPanelList.get(staffPanelList.size()-1));
			c.weighty = 0.0;
			staffsLayout.setConstraints(staffPanelList.get(staffPanelList.size()-1),c);
		}
		
		c = new GridBagConstraints();
		c.gridx = gridCurrX == 1 ? gridCurrX-- : gridCurrX++;
		c.gridy = gridCurrX == 0 ? gridCurrY++ : gridCurrY;
		c.fill = GridBagConstraints.HORIZONTAL;
		c.anchor = GridBagConstraints.NORTH;
		c.weightx = 1.0;
		c.weighty = 1.0;
		staffsPanel.add(staffPanel,c);
		staffPanelList.add(staffPanel);
		scrollPane.validate();
	}
	
	private void setListener() {
		
	}
	
	private StaffPanel getStaffPanelByName(String name){
		for (StaffPanel in : staffPanelList) {
			if(in.getName().equals(name))
				return in;
		}
		return null;
	}

	@Override
	public void propertyChange(PropertyChangeEvent arg0) {
		if(arg0.getPropertyName().equals(Data.GETCA)){
			NewProjectDialog.getInstance(this).setMaxValue(Float.parseFloat((String)arg0.getNewValue()));
			this.controlsPanel.setCA(Float.parseFloat((String) arg0.getNewValue()));
		}
		else if(arg0.getPropertyName().equals(Data.GETSTAFFSLIST)){
			ArrayList<Staff> staffList = (ArrayList<Staff>) arg0.getNewValue();
			for(int i = 0;i<staffList.size();i++){
				addNewStaff(staffList.get(i).function, staffList.get(i).name,staffList.get(i).lvl);
			}
		}
		else if(arg0.getPropertyName().equals(Data.INITGUINEWGAME)){
			Map<String,Object> prop = (Map<String, Object>) arg0.getNewValue();
			this.statusBar.setProgress(0);
			this.statusBar.setGamePlay(0);
			this.statusBar.setGraphism(0);
			this.statusBar.setSound(0);
			this.statusBar.setCreativity(0);
			this.controlsPanel.setGameTitle((String)prop.get("title"));
			this.controlsPanel.setGameGender((String)prop.get("genre"));
			this.controlsPanel.setGameType((String)prop.get("type"));
			this.controlsPanel.setCA(((Double)prop.get("ca")).floatValue());
			
			this.controlsPanel.setUnitSell(0f);
			this.controlsPanel.setBenefits(0f);
		}
		else if(arg0.getPropertyName().equals(Data.STEP1)){
			StaffListChooserDialog staffChooser = new StaffListChooserDialog(this);
			staffChooser.showIt((String)arg0.getNewValue(),XCommandAgent.STEP1);
		}
		else if(arg0.getPropertyName().equals(Data.STEP2)){
			StaffListChooserDialog staffChooser = new StaffListChooserDialog(this);
			staffChooser.showIt((String)arg0.getNewValue(),XCommandAgent.STEP2);
		}
		else if(arg0.getPropertyName().equals(Data.STEP3)){
			StaffListChooserDialog staffChooser = new StaffListChooserDialog(this);
			staffChooser.showIt((String)arg0.getNewValue(),XCommandAgent.STEP3);
		}
		else if(arg0.getPropertyName().equals("StaffCommandDialog")){
			initCommandFrame((String)((GuiEvent)arg0.getNewValue()).getParameter(0),
					(String)((GuiEvent)arg0.getNewValue()).getParameter(1),
					(Integer)arg0.getOldValue());
		}
		else if(arg0.getPropertyName().equals(Data.UPDATEPROGRESS)){
			HashMap<String,Integer> items = (HashMap<String, Integer>) arg0.getNewValue();
			
			statusBar.setGamePlay(items.get(Data.GAMEPLAYITEM));
			statusBar.setGraphism(items.get(Data.GRAPHISMITEM));
			statusBar.setSound(items.get(Data.SOUNDITEM));
			statusBar.setCreativity(items.get(Data.CREATIVITYITEM));
		}
		else if(arg0.getPropertyName().equals(Data.PROGRESS)){
			statusBar.setProgress((Integer) arg0.getNewValue());
		}
		else if(arg0.getPropertyName().equals(Data.STAFFACTION)){
			getStaffPanelByName((String)arg0.getOldValue()).addMessage((String)arg0.getNewValue());
		}
		else if(arg0.getPropertyName().equals(Data.NEWLVL)){
			getStaffPanelByName((String)arg0.getOldValue()).setLevel((Integer) arg0.getNewValue());
		}
		else if(arg0.getPropertyName().equals(Data.ENDGAME)){
			EndDialog endialog = new EndDialog();
			endialog.setSells((Integer)arg0.getNewValue());
			endialog.setVisible(true);
			this.controlsPanel.setUnitSell((Integer)arg0.getNewValue());
			this.controlsPanel.setBenefits(	((Integer)arg0.getNewValue())*Data.UNITPRICE/1000);
			this.controlsPanel.setNotoriety((Integer)arg0.getOldValue());
			GuiEvent e = new GuiEvent(this,XAgent.GETCA);
			agent.postGuiEvent(e);
		}
		else if(arg0.getPropertyName().equals(Data.GETHISTORICAL)){
			HistoricalDialog historicalDialog = HistoricalDialog.getInstance(this);
			historicalDialog.reinit();
			ArrayList<VideoGame> videoGameList = (ArrayList<VideoGame>) arg0.getNewValue();
			for(VideoGame curr : videoGameList){
				historicalDialog.addGame(curr.getTitle(),curr.getGameGenre(),
						curr.getGameType(),curr.getCost(),curr.getVentes(),curr.getProfits());
			}
			historicalDialog.setVisible(true);
		}
		else if(arg0.getPropertyName().equals(Data.STAMINA)){
			StaffPanel staffPanel = getStaffPanelByName((String) arg0.getOldValue());
			if(staffPanel != null){
				staffPanel.setBattery((Integer) arg0.getNewValue());
			}
		}
		else 
			System.err.println("Unknown Property Name : " +arg0.getPropertyName());
	}

	private void initCommandFrame(String staffName,String action,int type) {
		StaffCommandDialog staffCmdDialog = StaffCommandDialog.getInstance(staffName, action);
		staffCmdDialog.launchAgent(type);
	}


	public void setXAgent(XAgent xAgent) {
		this.agent = xAgent;
		GuiEvent e = new GuiEvent(this,XAgent.GETSTAFF);
		agent.postGuiEvent(e);
	}
	
	private void buildTheme(){
		//staffsPanel
		staffsPanel.setBackground(Color.DARK_GRAY);
		staffsPanel.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED,new Color(102,102,102),Color.WHITE));
	}


	public XAgent getXAgent() {
		return agent;
	}

}
